// components/cart-action/CartAction.js

Component({
  relations: {
    "../cart-item/CartItem": {
      type: 'child',
      linked(child) {
        this.children = this.children || []
        this.children.push(child)
        child.index = this.children.lengh
      },
      unlinked(child) {
        this.children = (this.children || []).filter(it => it !== child)
      }
    }
  },
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },

  externalClasses: [
    'custom-class',
  ],

  /**
   * 组件的属性列表
   */
  properties: {
    canPay: Boolean,
    checkAll: Boolean,
    checkedCount: {
      type: Number,
      value: 0
    },
    count: {
      type: Number,
      value: 0,
    },
    originPrice: Number,
    price: Number,
    currency: {
      type: String,
      value: '¥',
    },
  },

  observers: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    showDialog: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    ani() {
      // 如果已经打开购物车不需要动画
      if (this.data.show) {
        return
      }
      const animation = wx.createAnimation({
        delay: 0,
        duration: 200,
        timingFunction: 'ease'
      })
      animation.scale(1.5).step()
      this.setData({
        ani: animation.export()
      })
      animation.scale(1).step()
      setTimeout(() => {
        this.setData({
          ani: animation.export()
        })
      }, 300);
    },
    toggle() {
      this.setData({
        show: !this.data.show
      })
    },
    clean() {
      let that = this
      wx.showModal({
        title: '温馨提示',
        content: '清空购物车中所有商品？',
        success: function (res) {
          if (res.confirm) {
            that.doClean()
          }
        }
      })
    },
    checkAll() {
      this.triggerEvent('checkAll', !this.data.checkAll)
    },
    doClean() {
      this.triggerEvent('clean')
    },

    /**
     * 待支付
     */
    pay() {
      const { canPay } = this.data
      if (canPay) {
        this.triggerEvent('pay')
      }
    }
  }
})
